package com.demo.servlet.demo1216.servlet;

import com.demo.servlet.demo1216.entity.Account;
import com.demo.servlet.demo1216.service.AccountService;
import com.demo.servlet.demo1216.service.impl.AccountServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/demo1216/transfer.do")
public class TransferServlet extends HttpServlet {
    AccountService accountService = new AccountServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String to = request.getParameter("to").trim();
        String from = request.getParameter("from").trim();
        Double money = Double.valueOf(request.getParameter("money").trim());
        Account toAccount = accountService.queryByName(to);
        Account fromAccount = accountService.queryByName(from);
        accountService.transfer(fromAccount, toAccount, money);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
